package com.mofang.powerdekorhelper.fragment;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.activity.activity.ActivityDetailAct;
import com.mofang.powerdekorhelper.adapter.ActivityListAdapter;
import com.mofang.powerdekorhelper.base.MvpFragment;
import com.mofang.powerdekorhelper.base.recycleview.BaseRecycleViewAdapter;
import com.mofang.powerdekorhelper.base.recycleview.BaseRecycleViewList;
import com.mofang.powerdekorhelper.model.ActivityList;
import com.mofang.powerdekorhelper.model.PastActivityList;
import com.mofang.powerdekorhelper.model.ResultMessage4;
import com.mofang.powerdekorhelper.persenter.ActiveListPersenter;
import com.mofang.powerdekorhelper.utils.Constants;
import com.mofang.powerdekorhelper.utils.LoadPrograss;
import com.mofang.powerdekorhelper.utils.SharePerforenceUtils;
import com.mofang.powerdekorhelper.utils.T;
import com.mofang.powerdekorhelper.view.ActivityListView;
import com.mofang.powerdekorhelper.witget.MySwipeRefreshLayout;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by luofei on 2017/8/16 0016.
 * 栏目中心
 */

public class ActivityFragment extends MvpFragment<ActivityListView, ActiveListPersenter> implements ActivityListView, SwipeRefreshLayout.OnRefreshListener {

    public static ActivityFragment fragment;

    @BindView(R.id.index_activity_list_bg)
    LinearLayout ActivityNullBg;

    @BindView(R.id.activity_list_refresh)
    MySwipeRefreshLayout refreshLayout;

    // 轮播图
    @BindView(R.id.activity_list)
    BaseRecycleViewList activityRecycle;

    LoadPrograss loadPrograss;

    private int page = 1;
    private int pageSize = 10;
    private Integer userId;

    public static ActivityFragment getInstance() {
        if (fragment == null) {
            fragment = new ActivityFragment();
        }
        return fragment;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public View initView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_activity_layout, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void initData(Bundle savedInstanceState) {

        loadPrograss = new LoadPrograss(mContext);
        refreshLayout.setOnRefreshListener(this);
        userId = SharePerforenceUtils.getInstance(mContext).getShopId();
        presenter.getActivityList(page, pageSize, userId, "");
        presenter.getNode3Image(Constants.TestNode3Id);
    }

    @Override
    public void onRefresh() {
        presenter.getActivityList(page, pageSize, userId, "");
    }

    @Override
    public void setActivityList(final ActivityList activityList) {

        ActivityListAdapter adapter = new ActivityListAdapter(activityList.getResult().getData(), mContext, R.layout.activity_list_item_layout);

        adapter.setOnItemClickListner(new BaseRecycleViewAdapter.OnItemClickListner() {
            @Override
            public void onItemClickListner(ViewGroup parent, View v, int position) {

                ActivityList.Data data = (ActivityList.Data) v.getTag();

                startActivity(new Intent(mContext, ActivityDetailAct.class)
                        .putExtra("ActivityId", data.getId())
                        .putExtra("ActivityList", activityList));
            }
        });

        activityRecycle.setAdapter(adapter);

        hideProgress();
    }

    @Override
    public void showProgress() {
        loadPrograss.show();
    }

    @Override
    public void hideProgress() {
        loadPrograss.hide();
        refreshLayout.hide();
    }

    @Override
    public void toast(CharSequence s) {
        T.showShort(mContext, s);
    }

    @Override
    public void toast(int id) {

    }

    @Override
    public void showNullLayout() {
        toast("暂无活动");
        ActivityNullBg.setBackgroundResource(R.mipmap.shop_no_activity);
        hideProgress();
    }

    @Override
    public void setNode3Image(ResultMessage4 resultMessage4) {
        SharePerforenceUtils.getInstance(mContext).setNode3Image(resultMessage4.getResult());
        hideProgress();
    }

    @Override
    public void hideNullLayout() {
        hideProgress();
    }

    @Override
    public void showErrorLayout(View.OnClickListener listener) {
        hideProgress();
    }

    @Override
    public void hideErrorLayout() {
        hideProgress();
    }

    @Override
    public void onError(String errorInfo) {
        hideProgress();
        toast(errorInfo);
    }

    @Override
    public ActiveListPersenter initPresenter() {
        return new ActiveListPersenter();
    }

    @Override
    public void setPastActivityList(PastActivityList pastActivityList) {

    }
}
